package com.ybwh.springboot2.valid;

import lombok.extern.slf4j.Slf4j;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;


@Slf4j
public class UnionFieldValidator0 implements ConstraintValidator<UnionValids, Object> {

    private UnionValids unionValids;

    @Override
    public void initialize(UnionValids unionValid) {
        this.unionValids = unionValid;

    }

    @Override
    public boolean isValid(Object value, ConstraintValidatorContext context) {


        Class<? extends UnionFieldValidator>[] validators = unionValids.value();
        for (Class<? extends UnionFieldValidator> vClazz : validators) {
            try {
                UnionFieldValidator validator = vClazz.newInstance();
                UnionFieldValidator.Result r = validator.valid(value);

                if (!r.isValid()) {


                    //重写错误提示
                    context.disableDefaultConstraintViolation();
                    context.buildConstraintViolationWithTemplate(r.getMessage()).addConstraintViolation();


                    return false;
                }


            } catch (Exception e) {
                log.error("", e);
            }
        }

        return true;
    }


}
